package graph;

import geocoding.TripStop;
import geocoding.TripStopComparator;

import java.util.Collections;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Map.Entry;
import java.util.Set;



public class ALGOBuslineEdge extends ALGOBusNetEdge {
	private Hashtable<Integer,TripStop> departureTimes; // Fahrt -> Abfahrtszeit an der Quelle der Kante

	/**
     * default constructor
     */

    public ALGOBuslineEdge() {
    	super();
    	
    	lineName = "";
    	departureTimes = new Hashtable<Integer, TripStop>();
    	maxDuration = Integer.MIN_VALUE;
    }

    /**
     * constructor
     *
     * @param soid source node id
     * @param siid sink nodee id
     */

    public ALGOBuslineEdge(int soid, int siid) {
	    super(soid, siid);
	    
	    lineName = "";
    	departureTimes = new Hashtable<Integer, TripStop>();
    	maxDuration = Integer.MIN_VALUE;
    }

    /**
     * constructor
     *
     * @param soid source node id
     * @param siid sink nodee id
     * @param c costs for this edge
     */

    public ALGOBuslineEdge(int soid, int siid, double c) {
    	super(soid, siid, c);
    	
    	departureTimes = new Hashtable<Integer, TripStop>();
    	maxDuration = Integer.MIN_VALUE;
    }

    /**
     * constructor
     *
     * @param soid source node id
     * @param siid sink nodee id
     * @param c costs for this edge
     * @param ln name of bus line
     */

    public ALGOBuslineEdge(int soid, int siid, double c, String ln) {
    	super(soid, siid, c, ln);
    	
    	departureTimes = new Hashtable<Integer, TripStop>();
    	maxDuration = Integer.MIN_VALUE;
    }
    
    public void addTrip(Integer tripNum, TripStop ts) {
    	if (!departureTimes.contains(tripNum)) {
    		departureTimes.put(tripNum, ts);
    		if (maxDuration < ts.getT()) maxDuration = ts.getT();
    	}
    	else System.err.println("for line " + lineName + " trip " + tripNum + " already departs at: " + ts.getStopTime());
    }
 
    public String getTripTime(Integer tripNum) {
    	TripStop ts = departureTimes.get(tripNum);
    	
    	if (ts == null) return null;
    	else return ts.getStopTime();
    }
    
	
	public TripStop[] findNextTrips(int i, int [] depTime) {
		LinkedList<TripStop> tmp = new LinkedList<TripStop>();
		Enumeration<TripStop> en = departureTimes.elements();
		int len;
		while (en.hasMoreElements()) {
			tmp.add(en.nextElement());
		}
		
		len = (i < tmp.size() ? i : tmp.size());
		TripStop [] res = new TripStop[len];
		
		Collections.sort(tmp, new TripStopComparator());
		i = 0;
		
		for (TripStop t : tmp) {
			if (TripStopComparator.diffInMinutes(depTime[0], depTime[1], depTime[2], t) < 0) res[i++] = t;
			if (i == len) break;
		}
		
		return res;
	}
	
    public String toString() {
    	String res = lineName + "\n";
    	
    	Set<Entry<Integer, TripStop>> es = departureTimes.entrySet();
    	for (Entry<Integer, TripStop> entry : es) {
			res += "departs at: " + entry.getValue().getStopTime() + " (duration: " + entry.getValue().getT() + " sec)\n";
		}
    	
    	return res;
    }
}